package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Category;
import entities.Comment;
import entities.Event;

import managers.EntityManagerAux;
import managers.manEvent;

/**
 * Servlet implementation class SerEvent
 */
public class SerEvent extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	manEvent manE;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SerEvent() {
        super();
        // TODO Auto-generated constructor stub
        manE=new manEvent();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @EJB
    private EntityManagerAux eventz;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
	
			
		String types=(String)request.getParameter("types");
		String id=(String)request.getParameter("idEvent");		
		String idParticipant = (String)request.getParameter("idParticipant");
		String typesUser = (String)request.getParameter("typesUser")==null?"":(String)request.getParameter("typesUser");
		
		//New code to call from searches.
		if(types==null) {
			boolean found=false; //On cherche l'event selectionne par le client
			int i=0; //Parcourir la list des eventments 
			types="one";
			List<Event> lol= (List<Event>) request.getSession().getAttribute("myObjectId2");
			while(!found && i<lol.size()) {
				if(request.getParameter(""+i)!= null) { 
					found=true;
				}
				else 
					i++;
			}
			id =id.valueOf(lol.get(i).getIdEvent());
		}
		

		if("all".equals(types)){
			List<Event> allEvent=manE.allEvent();
			request.setAttribute("allEvent", allEvent);
			request.getRequestDispatcher("/allEvent.jsp").forward(request, response);
		}
		if("one".equals(types)){
			Event nEvent=manE.getEvent(Integer.parseInt(id));
			// recherche de ses comments
            Vector<Comment> comments = new Vector<Comment>();
            String idEvent= ""+nEvent.getIdEvent();
            comments.addAll(eventz.searchCommentsByEvent(idEvent));
            if (comments.equals(null)) {
                request.setAttribute("comments", new Vector<Comment>());
            } else {
                request.setAttribute("comments", comments);
            }

			System.out.println("typesUser"+typesUser);
			request.getSession().setAttribute("nEvent", nEvent); //Change
			request.setAttribute("typesUser", typesUser);
			request.getRequestDispatcher("/showEvent2.jsp").forward(request, response);
		}
		if("participer".equals(types)){
			boolean v;
			v=manE.addParticipant(Integer.parseInt(id), idParticipant);
			List<Event> allEvent=manE.allEvent();
			request.setAttribute("allEvent", allEvent);
			String message="Vous n'avez pas reussi a participer l'event";
			if (v==true){
				message="Vous avez reussi a participer l'event ";
			}
			request.setAttribute("message", message);
			request.getRequestDispatcher("/allEvent.jsp").forward(request, response);
			
		}
		if("noparticiper".equals(types)){
			boolean v;
			v=manE.delParticipant(Integer.parseInt(id), idParticipant);
			String message="Vous n'avez pas reussi a departiciper l'event";
			if (v==true){
				message="Vous avez reussi a departiciper l'event ";
			}
			System.out.println("Llego hasta aqui");
			//v=manE.addParticipant(Integer.parseInt(id), idParticipant);
			List<Event> allEvent=manE.allEvent();
			request.setAttribute("allEvent", allEvent);
			request.setAttribute("message", message);
			request.getRequestDispatcher("/allEvent.jsp").forward(request, response);	
		}
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}